package com.dm.library.widgets;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by tangjian on 2016/7/13.
 */
public class CommonUseScrollView extends ScrollView {

    public CommonUseScrollView(Context context) {
        super(context);
        mGestureDetector = new GestureDetectorCompat(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public CommonUseScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetectorCompat(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public CommonUseScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mGestureDetector = new GestureDetectorCompat(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }


    private GestureDetectorCompat mGestureDetector;
    //    View.OnTouchListener mGestureListener;

    private float downX = 0;
    private float downY = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(ev.getY() - downY) - Math.abs(ev.getX() - downX) > 50) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(ev.getY() - downY) - Math.abs(ev.getX() - downX) > 50) {
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }

    private Handler mHandler = new Handler();

    /**
     * 滚动状态   IDLE 滚动停止  TOUCH_SCROLL 手指拖动滚动     FLING滚动
     */
    enum ScrollType {
        IDLE, TOUCH_SCROLL, FLING
    }

    /**
     * 记录当前滚动的距离
     */
    private int currentY = -9999999;

    /**
     * 当前滚动状态
     */
    private ScrollType scrollType = ScrollType.IDLE;

    /**
     * 滚动监听间隔
     */
    private int scrollDelay = 10;

    /**
     * 滚动监听runnable
     */
    private Runnable scrollRunnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (getScrollY() == currentY) {
                //滚动停止  取消监听线程
                scrollType = ScrollType.IDLE;
                if (null != mOnScrollStateChangedListener) {
                    mOnScrollStateChangedListener.onScrollStateChanged(getScrollY());
                }
                mHandler.removeCallbacks(this);
                return;
            } else {
                //手指离开屏幕    view还在滚动的时候
                scrollType = ScrollType.FLING;
                if (null != mOnScrollStateChangedListener) {
                    mOnScrollStateChangedListener.onScrollStateChanged(getScrollY());
                }
            }
            currentY = getScrollY();
            mHandler.postDelayed(this, scrollDelay);
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                this.scrollType = ScrollType.TOUCH_SCROLL;
                if (null != mOnScrollStateChangedListener) {
                    mOnScrollStateChangedListener.onScrollStateChanged(getScrollY());
                }
                //手指在上面移动的时候   取消滚动监听线程
                mHandler.removeCallbacks(scrollRunnable);
                break;
            case MotionEvent.ACTION_UP:
                //手指移走的时候
                mHandler.post(scrollRunnable);
                break;
        }
        return super.onTouchEvent(ev);
    }

    private OnScrollStateChangedListener mOnScrollStateChangedListener;

    public void setOnScrollStateChangedListener(OnScrollStateChangedListener onScrollStateChangedListener) {
        mOnScrollStateChangedListener = onScrollStateChangedListener;
    }

    public interface OnScrollStateChangedListener {
        void onScrollStateChanged(int scrollY);
    }
}
